package com.liao.controller;


import com.liao.bean.Code;
import com.liao.bean.result.Result;
import com.liao.dao.CodeDao;
import com.liao.service.CodeService;
import com.liao.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@CrossOrigin
@RestController
@RequestMapping("/sms")
public class SmsController {


    @Autowired
    private SmsUtil smsUtil;

    @Autowired
    private CodeDao codeDao;

    @RequestMapping("/getPhoneVerificationCode")
    public Result getPhoneVerificationCode(String phoneNum , HttpSession session) throws Exception {

        //根据手机号发送短信
        smsUtil.sendSms(phoneNum,session);

        return new Result(true,20010,"短信发送成功",null);
    }

    @RequestMapping("/login")
    public Result login(String phone,String code,HttpSession session) throws Exception {
        //根据手机从数据库查询保存的验证码
        Code existCode = codeDao.selectCodeByPhoneNum(phone);
        if (existCode.getCode().equals(code)) {
            return new Result(true,20020,"登录成功",null);
        } else {
            return new Result(false,20021,"登录失败",null);
        }
    }


}
